package com.xhwl.data.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.dto.cent.sdata.AppVersionDTO;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.AppVersion;
import com.xhwl.common.query.cent.sdata.AppVersionQuery;
import com.xhwl.data.dao.IAppVersionDao;
import com.xhwl.data.service.IAppVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * @Author JCccc
 * @Description
 * @Date 2021/7/17 10:55
 */
@Service
public class AppVersionServiceImpl extends ServiceImpl<IAppVersionDao, AppVersion> implements IAppVersionService {
    @Autowired
    IAppVersionDao appVersionDao;

    @Override
    public ResultJson create(AppVersion appVersion) {
        boolean save = save(appVersion);
        if (!save) {
            return ResultJson.fail("操作失败");
        }
        return ResultJson.success(appVersion.getId());
    }

    @Override
    public ResultJson update(AppVersion appVersion) {

        //更新信息
        Integer id = appVersion.getId();
        AppVersion appVersionDb = getById(id);
        if (null == appVersionDb) {
            return ResultJson.fail("不存在当前版本信息");
        }
        String newVersion = appVersionDb.getNewVersion();
        appVersion.setPrevVersion(newVersion);
        UpdateWrapper<AppVersion> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", appVersion.getId());
        int effectNumUpdate = appVersionDao.update(appVersion, updateWrapper);
        if (effectNumUpdate != 1) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultJson.fail("更新账号数据失败");
        }
        return ResultJson.success("操作成功");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public ResultJson delete(Integer id) {
        UpdateWrapper<AppVersion> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("is_deleted", true);
        update(updateWrapper);
        return ResultJson.success("操作成功");
    }

    /**
     * 获取列表 不分页
     *
     * @param appVersionQuery
     * @return
     */
    @Override
    public List<AppVersionDTO> list(AppVersionQuery appVersionQuery) {
        if (null == appVersionQuery) {
            appVersionQuery = new AppVersionQuery();
        }
        return appVersionDao.getList(appVersionQuery);
    }

    /**
     * 获取列表 分页
     *
     * @param appVersionQuery
     * @param page
     * @return
     */
    @Override
    public Page<AppVersionDTO> pageList(AppVersionQuery appVersionQuery, Page page) {
        if (null == appVersionQuery) {
            appVersionQuery = new AppVersionQuery();
        }
        return appVersionDao.getPageList(page, appVersionQuery);
    }

    /**
     * 检测当前版本信息
     *
     * @param appVersionQuery
     * @return
     */
    @Override
    public AppVersion getNewestVersion(AppVersionQuery appVersionQuery) {
        return appVersionDao.getNewestVersion(appVersionQuery);
    }
}
